/**
 * Copyright (C) 2011 - @le.nguyen
 * @Created Jul 27, 2011
 *
 * TODO
 */
package com.conan.dailyflickr.app;

import java.util.HashMap;
import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import com.conan.dailyflickr.R;
import com.conan.dailyflickr.activity.DailyImageDialog;
import com.conan.dailyflickr.activity.DowloadedPhotos;

/**  */
public class ProgressNotification {
	private static ProgressNotification instance;
	private static Context mContext;
	HashMap<String,NotifyItem > mNotificaitons = new HashMap<String, ProgressNotification.NotifyItem>();
	
	
	public class NotifyItem{
		private int id;
		private String title;

		public Notification _notification = null;
		public NotifyItem(String strTitle) {
			id = (int)System.currentTimeMillis();
			if(!TextUtils.isEmpty(strTitle)){
				if(strTitle.length() > 40){
					title = strTitle.substring(0,40).concat("...");
				}else{
					title = strTitle;
				}
			}
		}
	}
	
	public static ProgressNotification getInstance(Context context){
		mContext = context;
		if(instance == null){
			 instance = new ProgressNotification();
		}
		return instance;
	}


	public boolean hasDownloading(){
		return mNotificaitons.size() > 0 ? true : false;
	}
	public void showNotification(String url, String title){
		NotifyItem notify = new NotifyItem(title);
		
		if(title.length() > 50)
			title = title.substring(0,50);
		// configure the notification
		final Notification notification = new Notification(
				 android.R.drawable.stat_sys_download, title, 
				System.currentTimeMillis());
		
		Intent intent = new Intent(mContext, DailyImageDialog.class);
		final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
		notification.contentIntent = pendingIntent;
		notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;
		notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.download_progress);
				
		notification.contentView.setImageViewResource(R.id.status_icon,  android.R.drawable.stat_sys_download);
		notification.contentView.setTextViewText(R.id.title_text, notify.title);
		notification.contentView.setTextViewText(R.id.status_text, "Dowloading");
		Random ran = new Random();
		notification.contentView.setProgressBar(R.id.status_progress, 100, ran.nextInt(90), false);

		final NotificationManager notificationManager = (NotificationManager)
		mContext.getSystemService(mContext.NOTIFICATION_SERVICE);

		notificationManager.notify(notify.id, notification);
		
		notify._notification = notification;
		mNotificaitons.put(url, notify);
	}

	public void updateNotification(String url, int progress){
		NotifyItem notify = mNotificaitons.get(url);
		if(notify == null) return;

		final Notification notification = notify._notification;
		
		final NotificationManager notificationManager = (NotificationManager)
		mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
		
		Intent intent = new Intent(mContext, DowloadedPhotos.class);
		final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
		notification.contentIntent = pendingIntent;
		
		// notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
		// inform the progress bar of updates in progress
		notificationManager.notify(notify.id, notification);
	}

	public void updateNotificationDone(String url, boolean success){
		NotifyItem notify  = mNotificaitons.remove(url);
		Log.d(PhotoDailyInfo.TAG, "updateNotificationDone >> " + notify);
		if(notify == null) return;

		final NotificationManager notificationManager = (NotificationManager)
		mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
		
		notificationManager.cancel(notify.id);
		
		final Notification notification = new Notification(
				android.R.drawable.stat_sys_download_done, notify.title, 
				System.currentTimeMillis());		
		
		Intent intent = new Intent(mContext, DowloadedPhotos.class);
		final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
		notification.contentIntent = pendingIntent;
		notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.download_progress);
		
		notification.contentView.setImageViewResource(R.id.status_icon,  android.R.drawable.stat_sys_download_done);
		notification.contentView.setTextViewText(R.id.title_text, notify.title);
		// String titleStatus = isWallpaper? "Set wallpaper" : "Dowload";
		if(success){
			notification.contentView.setTextViewText(R.id.status_text, "Progress success !!!");
		}else{
			notification.contentView.setTextViewText(R.id.status_text, "Progress failed !!!");
		}
		// notification.contentView.setViewVisibility(R.id.status_progress, View.GONE);
		notificationManager.notify(notify.id, notification);
	}
	
	public void updateAllNotificationsFailed(){
		final NotificationManager notificationManager = (NotificationManager)
		mContext.getSystemService(mContext.NOTIFICATION_SERVICE);

		Intent intent = new Intent(mContext, DailyImageDialog.class);
		final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

		NotifyItem notify;
		for(String url : mNotificaitons.keySet()){
			notify = mNotificaitons.get(url);
			final Notification notification = notify._notification;					

			notification.contentIntent = pendingIntent;
			notification.contentView.setTextViewText(R.id.title_text, notify.title);		
			notification.contentView.setTextViewText(R.id.status_text, "Dowload failed !!!");
			// notification.contentView.setViewVisibility(R.id.status_progress, View.GONE);
			notificationManager.notify(notify.id, notification);
		}
		
		mNotificaitons.clear();
	}
	
	public void deleteAllNotifications(){
		final NotificationManager notificationManager = (NotificationManager)
		mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
		
		NotifyItem notify;
		for(String url : mNotificaitons.keySet()){
			notify  = mNotificaitons.get(url);
			notificationManager.cancel(notify.id);
		}
		
		mNotificaitons.clear();
	}
	
	
	
	public void deleteNotification(String url){
		NotifyItem notify  = mNotificaitons.remove(url);
		final NotificationManager notificationManager = (NotificationManager)
		mContext.getSystemService(mContext.NOTIFICATION_SERVICE);
		
		notificationManager.cancel(notify.id);
	}
}
